package com.casic.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年12月21日 下午12:53:33
 */
public class DateUtils {
	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";
	/** 时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	
	public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if(date != null){
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 周期开始时间
     * @param date
     * @return
     */
    public static Date getStartTimeForMonth(Date date){
        Calendar curr = Calendar.getInstance();
        curr.setTime(date);
        int year=curr.get(Calendar.YEAR);
        int month=curr.get(Calendar.MONTH);
        curr.set(Calendar.YEAR, year);
        curr.set(Calendar.MONTH, month);
        curr.set(Calendar.DATE, 1);
        Date startDate=curr.getTime();
        String dateStr=format(startDate)+" 00:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
        try {
            startDate=sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return startDate;
    }

    /**
     * 周期结束时间
     * @param date
     * @param months
     * @return
     */
    public static Date getEndTimeForMonth(Date date,int months){
        Calendar curr = Calendar.getInstance();
        curr.setTime(date);
        int year=curr.get(Calendar.YEAR);
        int month=curr.get(Calendar.MONTH);
        if((month+months)>11){
            year+=1;
            month=(month+(months-1))-12;
        }else{
            month+=months-1;
        }
        curr.set(Calendar.YEAR, year);
        curr.set(Calendar.MONTH, month);
        curr.set(Calendar.DAY_OF_MONTH, curr.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date endDate=curr.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
        String dateStr=format(endDate)+" 23:59:59";
        try {
            endDate=sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return endDate;
    }

    /**
     * 获取所在周的第一天
     * @param date
     * @return
     */
    public static Date getStartTimeForWeek(Date date){
        LocalDate inputDate = LocalDate.parse(format(date));
        TemporalAdjuster FIRST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate.getDayOfWeek().getValue()-DayOfWeek.MONDAY.getValue()));
        String dateStr=inputDate.with(FIRST_OF_WEEK).toString()+" 00:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
        Date startDate=null;
        try {
            startDate=sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return startDate;
    }

    /**
     * 获取所在周的最后一天
     * @param date
     * @return
     */
    public static Date getEndTimeForWeek(Date date){
        LocalDate inputDate = LocalDate.parse(format(date));
        TemporalAdjuster LAST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
        String dateStr=inputDate.with(LAST_OF_WEEK).toString()+" 23:59:59";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
        Date endDate=null;
        try {
            endDate=sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return endDate;
    }

    public static void main(String[] args) {
        int cycle=4;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try{
            date = sdf.parse("2018-11-2");//初始日期
        }catch(Exception e){
        }
        System.out.print("周期为："+cycle+"个月\n");
        System.out.print("周期开始时间:"+DateUtils.format(DateUtils.getStartTimeForMonth(date),DATE_TIME_PATTERN)+"\n");
        System.out.print("周期结束时间:"+DateUtils.format(DateUtils.getEndTimeForMonth(date,4),DATE_TIME_PATTERN)+"\n");
        System.out.print("一周开始时间:"+DateUtils.format(DateUtils.getStartTimeForWeek(date),DATE_TIME_PATTERN)+"\n");
        System.out.print("一周结束时间:"+DateUtils.format(DateUtils.getEndTimeForWeek(date),DATE_TIME_PATTERN));
    }
}
